import {Component} from '@angular/core';
import {
    NavController, Events, AlertController, ToastController, Platform, LoadingController,
    ActionSheetController, NavParams
} from 'ionic-angular';
import {AddCountWayPage} from "./add-count-way/add-count-way";
import {Log} from "../../framework/utils/Log";
import {BasePage} from "../BasePage";
import {SystemService} from "../service/SystemService";

@Component({
    selector: 'page-applyList',
    templateUrl: 'applyList.html',
    providers: [SystemService]
})
export class ApplyListAPage extends BasePage{
    private static TAG:string="ApplyListAPage";
    private _tabEveBool: boolean = true;
    applyBool: boolean = false;
    expendBool: boolean = false;
    assistBool: boolean = false;
    closeAccountBool: boolean = false;
    private selectId: any = 0;
    addBool: boolean = false;
    detailsInfo:any;
    applyInfo:any;
    closeAccountInfo:any;
    zhichuInfo:any;
    assistInfo:any;
    spType:string;
    public tabs: Array<any> = [
        {
            name: '报销申请'
        },
        {
            name: '支出信息'
        },
        {
            name: '辅助事项'
        },
        {
            name: '结算信息'
        }
    ];

    constructor(public navCtrl: NavController,
                public event: Events,
                public navParams: NavParams,
                public sys: SystemService,
                public platform: Platform,
                public loadingCtrl: LoadingController,
                public toastCtrl: ToastController,
                public alertCtrl: AlertController) {
        super(platform, loadingCtrl, toastCtrl, alertCtrl);
        this.detailsInfo = navParams.data.data;
        this.spType =navParams.data.type;
        Log.printObj(ApplyListAPage,'result==',this.detailsInfo);

        this.event.subscribe('tab', (res) => {
            if (res == 'tab3') {
                this._tabEveBool = false;
                this.event.unsubscribe('tab');
                this.tabName('结算信息', 3);
            }
            else {
                this._tabEveBool = true;
            }
        })
    }
    async ngOnInit() {
        if(this.spType=="4"){
            this.getCheckDetail(this.detailsInfo);
        }

    }
    ionViewDidEnter() {
        if (this._tabEveBool){
            this.tabName('报销申请', 0);
        }
    }
    async getCheckDetail(_d) {
        let data: any = {
            BianHao: _d.DanJuBH,
            LiuChengId: _d.LiuChengId,
            RenWuID: _d.RenWuID,
            DangQianJD: _d.DangQianJD
        };
        await this.sys.getCheckDetail(data).subscribe((res: any) => {
            Log.printObj(ApplyListAPage.TAG, 'ApplyInfo printObj==', res);
            if (res.msgCode == '1') {
                this.applyInfo = res.DanJuXX;
                this.assistInfo =res.FuZhuXX;
                this.closeAccountInfo = res.JieSuanXX;
                this.zhichuInfo = res.ZhiChuMX;

            } else {
                this.showError('数据请求失败');
            }

        }, error => {
            this.showError('数据请求失败');
        })
    }

    tabName(name, id) {
        this.allTabsBool();
        let tabId: any = document.getElementById('apply_' + id).style;
        let _selectId: any = document.getElementById('apply_' + this.selectId).style;
        let tabTxtId: any = document.getElementById('applyName_' + id).style;
        let setabTxtId: any = document.getElementById('applyName_' + this.selectId).style;
        _selectId.backgroundColor = 'rgba(0,0,0,0)';
        setabTxtId.color = "#666666";

        switch (name) {
            case '报销申请':
                this.addBool = false;
                this.selectId = 0;
                this.applyBool = true;
                break;
            case '支出信息':
                this.addBool = false;
                this.selectId = 1;
                this.expendBool = true;
                break;
            case '辅助事项':
                this.addBool = false;
                this.selectId = 2;
                this.assistBool = true;
                break;
            case '结算信息':
                this.addBool = true;
                this.selectId = 3;
                this.closeAccountBool = true;
                break;
        }
        tabId.backgroundColor = '#fd9827';
        tabTxtId.color = "#1c70bf";
    }

    allTabsBool() {
        this.applyBool = false;
        this.expendBool = false;
        this.assistBool = false;
        this.closeAccountBool = false;
    }

    onAddCountWay() {
        this.navCtrl.push(AddCountWayPage);
    }

    getChildEvent(data) {
        let name: any = data.name;
        let id: any = data.id;
        this.tabName(name, id);
    }

}
